import Vue from 'vue'
import App from './App.vue'
import router from './router'
import Vuex from 'vuex'
Vue.config.productionTip = false
Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    goodList: [
      {
        name: "诸葛亮",
        price: 1000,
        num: 1,
        checked: false,
      },
      {
        name: "蔡文姬",
        price: 1500,
        num: 1,
        checked: false,
      },
      {
        name: "妲己",
        price: 2000,
        num: 1,
        checked: false,
      },
      {
        name: "鲁班",
        price: 2200,
        num: 1,
        checked: false,
      },
    ],
  },
  getters: {
    Price(state) { 
      return  state.goodList.map (item => { 
         return item.price * item.num
      })
    },
    allPrice(state) { 
    return  state.goodList.reduce((sum, item) => { 
        if (item.checked) { 
         sum += item.price*item.num
        }
        return sum
      },0)
    }
  },
  mutations: {
    addFn(state,index) { 
    state.goodList[index].num++
    },
    dircent(state,index) { 
      if (state.goodList[index].num <=1) {
        state.goodList[index].num == 1
      } else { 
        state.goodList[index].num--
      }
    },
    delBtn(state,name) { 
    state.goodList= state.goodList.filter((item)=>item.name!=name)
    }
  }
})

new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app')
